package com.github.cdisk.xtrans.component;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.github.cdisk.xtrans.annotiation.Attribute;
import com.github.cdisk.xtrans.annotiation.Component;
import com.github.cdisk.xtrans.annotiation.ConnectLine;
import com.github.cdisk.xtrans.core.BaseComponent;
import com.github.cdisk.xtrans.core.ConfigFile;
import com.github.cdisk.xtrans.core.GlobalVariables;
import com.github.cdisk.xtrans.exception.BuildException;

/**
 * 判断选择
 * @author Cdisk
 *
 */
@Component(value="判断选择", icon="arrow_switch.png", group="常用元件")
public class SwitchCase extends BaseComponent {

	private static final Logger LOGGER = Logger.getLogger(SwitchCase.class);

	/** 判断条件1 */
	@Attribute(value="条件1, a == 1", group="执行")
	protected String condition1 = null;

	/** 条件1满足时执行动作 */
	@Attribute(value="执行1, a != 4", group="执行")
	@ConnectLine(value="条件1")
	protected BaseComponent execute1 = null;

	/** 判断条件2 */
	@Attribute(value="条件2", group="执行")
	protected String condition2 = null;

	/** 条件1满足时执行动作 */
	@Attribute(value="执行2", group="执行")
	@ConnectLine(value="条件2")
	protected BaseComponent execute2 = null;

	/** 不满足上述条件的时候执行动作 */
	@Attribute(value="默认执行", group="执行")
	@ConnectLine(value="默认执行")
	protected BaseComponent defaultExecute = null;



	@Override
	public void build(ConfigFile config, GlobalVariables variables) throws BuildException {
		super.build(config, variables);
	}

	@Override
	public int execute(Map<String, Object> input) {
		try {
			boolean executed = false;
			if (condition(condition1, input)) {
				executed = true;
				if (execute1 != null) {
					execute1.execute(input);
				}
			}
			if (condition(condition2, input)) {
				executed = true;
				if (execute2 != null) {
					execute2.execute(input);
				}
			}
			if (!executed) {
				// 执行默认处理
				if (defaultExecute != null) {
					defaultExecute.execute(input);
				}
			}
			
			/* 执行成功处理 */
			if (this.success != null) {
				this.success.execute(input);
			}
			LOGGER.info("执行请求:" + this.id + ", 执行成功.");
			return EXECUTE_SUCCESS;
		} catch (Exception e) {
			/* 执行错误处理 */
			Map<String, Object> __map = new HashMap<String, Object>();
			if (this.error != null) {
				__map.put("$errorMessage", e.getMessage());
				this.error.execute(input);
			}
			e.printStackTrace();
			LOGGER.info("执行请求:" + this.id + ", 执行错误, " + e.getClass().getSimpleName() + ": " + e.getMessage());
			return EXECUTE_ERROR;
		}
	}
	
	/**
	 * 简易条件处理器，太简单了，自己都看不下去，将就用
	 * @param cond  目前只支持  a == 1 和 a != 2两种判断
	 * @param input
	 * @return
	 */
	private boolean condition(String cond, Map<String, Object> input) {
		if (StringUtils.isEmpty(cond)) {
			return false;
		}
		String[] conds = cond.split("\\s+");
		String key = conds[0];
		String logic = conds[1];
		String value = conds[2];

		String inputValue = String.valueOf(input.get(key));
		if (logic.equals("==")) {
			return inputValue.equals(value);
		} else if (logic.equals("!=")) {
			return !inputValue.equals(value);
		}
		
		return false;
	}

	public String getCondition1() {
		return condition1;
	}

	public void setCondition1(String condition1) {
		this.condition1 = condition1;
	}

	public BaseComponent getExecute1() {
		return execute1;
	}

	public void setExecute1(BaseComponent execute1) {
		this.execute1 = execute1;
	}

	public String getCondition2() {
		return condition2;
	}

	public void setCondition2(String condition2) {
		this.condition2 = condition2;
	}

	public BaseComponent getExecute2() {
		return execute2;
	}

	public void setExecute2(BaseComponent execute2) {
		this.execute2 = execute2;
	}

	public BaseComponent getDefaultExecute() {
		return defaultExecute;
	}

	public void setDefaultExecute(BaseComponent defaultExecute) {
		this.defaultExecute = defaultExecute;
	}

}
